package com.example.kehoobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.Admin;
import com.example.kehoobackend.domain.DayInfo;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.persistance.AdminMapper;
import com.example.kehoobackend.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserService userService;

    @Autowired
    QuestionService questionService;

    @Autowired
    AnswerService answerService;

    @Autowired
    DayInfoService dayInfoService;


    @Override
    public CommonResponse<Map> login(String username, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("password",password);
        Admin admin = getOne(queryWrapper);

        if(admin == null){
            return CommonResponse.createForError("登录失败");
        }else {
            Map<String, Object> map = new HashMap<>();
            admin.setPassword(null);
            String token = UUID.randomUUID().toString();

            //清除之前的token
            redisTemplate.delete("admin"+admin.getAdmin_id()+"token");
            //向redis中添加token缓存，标识为"admin"+id+"token"
            redisTemplate.opsForValue().set("admin"+admin.getAdmin_id()+"token",token);

            map.put("Admin",admin);
            map.put("token",token);


            //更新登录时间
            //TODO:这里逻辑可能不对
            LocalDateTime now = LocalDateTime.now();
            String mysqlDatetime = now.format(formatter);
            admin.setLogin_time(mysqlDatetime);
            updateById(admin);

            return CommonResponse.createForSuccess("登录成功！",map);
        }
    }

    @Override
    public boolean logout(Integer admin_id) {
        return redisTemplate.delete("admin"+admin_id+"token");
    }

    @Override
    public CommonResponse<Map<String, String>> getAdminInfo(Integer admin_id) {
        Map<String, String> map = new HashMap<>();

        //用户名，登录时间与职位
        Admin admin = getById(1);
        map.put("username",admin.getUsername());
        map.put("login_time",admin.getLogin_time());
        map.put("position","超级管理员");

        //当前用户、问题、回答总量
        long all_users_count,all_questions_count,all_answers_count;
        all_users_count = userService.count();
        all_questions_count = questionService.count();
        all_answers_count = answerService.count();
        map.put("all_users_count",String.valueOf(all_users_count));
        map.put("all_questions_count",String.valueOf(all_questions_count));
        map.put("all_answers_count",String.valueOf(all_answers_count));

        //今日新增用户、问题、回答数
        long today_users_count,today_questions_count,today_answers_count;
        String currentDate = String.valueOf(LocalDate.now());
        DayInfo dayInfo = dayInfoService.getById(currentDate);
        today_users_count = dayInfo.getNew_users_count();
        today_questions_count = dayInfo.getNew_questions_count();
        today_answers_count = dayInfo.getNew_answers_count();
        map.put("today_users_count",String.valueOf(today_users_count));
        map.put("today_questions_count",String.valueOf(today_questions_count));
        map.put("today_answers_count",String.valueOf(today_answers_count));

        return CommonResponse.createForSuccess(map);
    }

    @Override
    public boolean updateLastLoginTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        Admin admin = getById(1);
        admin.setLogin_time(String.valueOf(localDateTime));

        return updateById(admin);
    }


}
